"""
@author: xiangping
@contact: xiangpingbu@gmail.com
@time: 2020/1/7 7:36 下午
@file: sys_service
@Desc:
"""
import pickle

import pandas as pd

from app.configs import config_base_dir
from app.controllers.Exception.iron_exception import GaoLuInitException
from app.exts import data_container
from app.services.business.gaolu_biz_service import gaolu_calculate_dist, gaolu_calculate_quantile
from app.services.dal.gaolu_dao_service import gaolu_history_between_json
from app.services.dal.hanfan_dao_service import hanfan_history_between_json
from app.services.business.hanfan_biz_service import hanfan_calculate_dist, hanfan_calculate_quantile



def init_data_container(app, is_loaded=False, is_persist=False):
    '''
    :param app:
    :param is_loaded: True 表示从pickle文件读取，不新生成
    :param is_persist: True 表示，将新生成的 dict, 以文件形势存储
    :return:
    '''

    if app.config['GAOLU_PARAM_DIST_ENABLE'] or app.config['GAOLU_PARAM_QUAN_ENABLE']:
        if app.config['GAOLU_PARAM_HISTORY_START'] is None or app.config['GAOLU_PARAM_HISTORY_END'] is None:
            raise GaoLuInitException(message="GAOLU_PARAM_HISTORY setting missing")

        if is_loaded:
            data_container['gaolu_param_distribution'] = pickle.load(open(config_base_dir + "/dist_dict.pk", "rb"))
            data_container['gaolu_param_quantile'] = pickle.load(open(config_base_dir + "/quantile_dict.pk", "rb"))
        else:
            history = gaolu_history_between_json(app.config['GAOLU_PARAM_HISTORY_START'],
                                                 app.config['GAOLU_PARAM_HISTORY_END'])
            history_df = pd.read_json(history)
            if app.config['GAOLU_PARAM_DIST_ENABLE']:
                dist_dict = gaolu_calculate_dist(start=app.config['GAOLU_PARAM_HISTORY_START'],
                                                 end=app.config['GAOLU_PARAM_HISTORY_END'],
                                                 df=history_df,
                                                 bin_num=app.config['GAOLU_PARAM_HISTORY_BINS'])
                data_container['gaolu_param_distribution'] = dist_dict
                if is_persist:
                    pickle.dump(dist_dict, open(config_base_dir + "/dist_dict.pk", "wb"))
            if app.config['GAOLU_PARAM_QUAN_ENABLE']:
                quantile_dict = gaolu_calculate_quantile(start=app.config['GAOLU_PARAM_HISTORY_START'],
                                                         end=app.config['GAOLU_PARAM_HISTORY_END'],
                                                         df=history_df)
                data_container['gaolu_param_quantile'] = quantile_dict
                if is_persist:
                    pickle.dump(quantile_dict, open(config_base_dir + "/quantile_dict.pk", "wb"))

    if app.config['HANFAN_PARAM_DIST_ENABLE'] or app.config['HANFAN_PARAM_QUAN_ENABLE']:
        if app.config['HANFAN_PARAM_HISTORY_START'] is None or app.config['HANFAN_PARAM_HISTORY_END'] is None:
            raise GaoLuInitException(message="HANFAN_PARAM_HISTORY setting missing")

        if is_loaded:
            data_container['hanfan_param_distribution'] = pickle.load(
                open(config_base_dir + "/hanfan_dist_dict.pk", "rb"))
            data_container['hanfan_param_quantile'] = pickle.load(
                open(config_base_dir + "/hanfan_quantile_dict.pk", "rb"))
        else:
            history = hanfan_history_between_json(app.config['HANFAN_PARAM_HISTORY_START'],
                                                  app.config['HANFAN_PARAM_HISTORY_END'])
            history_df = pd.read_json(history)
            if app.config['HANFAN_PARAM_DIST_ENABLE']:
                dist_dict = hanfan_calculate_dist(start=app.config['HANFAN_PARAM_HISTORY_START'],
                                                  end=app.config['HANFAN_PARAM_HISTORY_END'],
                                                  df=history_df,
                                                  bin_num=app.config['HANFAN_PARAM_HISTORY_BINS'])
                data_container['hanfan_param_distribution'] = dist_dict
                if is_persist:
                    pickle.dump(dist_dict, open(config_base_dir + "/hanfan_dist_dict.pk", "wb"))
            if app.config['HANFAN_PARAM_QUAN_ENABLE']:
                quantile_dict = hanfan_calculate_dist(start=app.config['HANFAN_PARAM_HISTORY_START'],
                                                      end=app.config['HANFAN_PARAM_HISTORY_END'],
                                                      df=history_df,
                                                      bin_num=app.config['HANFAN_PARAM_HISTORY_BINS'])
                data_container['hanfan_param_quantile'] = quantile_dict
                if is_persist:
                    pickle.dump(quantile_dict, open(config_base_dir + "/hanfan_quantile_dict.pk", "wb"))